package swu.edu.cn.javaee.service;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.entity.CollectInfo;
import swu.edu.cn.javaee.entity.Comment;
import swu.edu.cn.javaee.entity.CommentInfo;
import swu.edu.cn.javaee.mapper.CommentMapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static swu.edu.cn.javaee.component.Signal.*;

@Service
public class CommentService {
    @Resource
    private CommentMapper commentMapper;


    //新增评论
    public String saveComment(String comment,String news_id,String user_id,String parent_id,String nickname){
        Comment comment1= new Comment();
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        comment1.setComment(comment);
        comment1.setNews_id(news_id);
        comment1.setUser_id(user_id);
        comment1.setParent_id(parent_id);
        comment1.setNickname(nickname);
        comment1.setDate(format.format(date));
        if (news_id!=null){
            commentMapper.saveComment(comment1);
            System.out.println(comment1);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_COMMENT;
        }
    }

    //用户查看所有评论
    public List<Comment> queryCommentByUid(String user_id){
        List<Comment> comments=commentMapper.queryCommentByUid(user_id);
        System.out.println(comments);
        return comments;
    }

    //根据ID查询评论
    public Comment queryCommentById(String comment_id){
        Comment comment=commentMapper.queryCommentById(comment_id);
        return comment;
    }

    //查看文章下所有评论
    public List<Comment> queryListByNews_id(String news_id){
        List<Comment> commentList=commentMapper.queryListByNews_id(news_id);
        System.out.println(commentList);
        return commentList;
    }

    //分页查询
    public CommentInfo queryListLimit(String news_id,String page,String limit){
        Integer start=Integer.valueOf(page);
        Integer limits=Integer.valueOf(limit);
        CommentInfo commentInfo=new CommentInfo();
        int counts=commentMapper.counts(news_id);
        List<Comment> commentList=commentMapper.queryListByNews_id(news_id);
        if (commentList.size()!=0){
            commentInfo.setCode("0");
            commentInfo.setMsg("");
            commentInfo.setCount(String.valueOf(counts));
            commentInfo.setData(commentList);
            System.out.println(commentInfo);
        }
        else {
            commentInfo.setCode("1");
        }
        return commentInfo;
    }


    //user修改评论
    public String updateComment(Comment comment){
        Comment result=commentMapper.queryCommentById(comment.getComment_id());
        if (result!=null){
            result.setComment(comment.getComment());
            commentMapper.updateComment(result);
            System.out.println(commentMapper.queryCommentById(comment.getComment_id()));
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_NO_COMMENT;
        }
    }

    //删除评论
    public String deleteComment(String comment_id){
        Comment comment1=commentMapper.queryCommentById(comment_id);
        if (comment1!=null){
            commentMapper.deleteComment(comment1);
            System.out.println("删除成功！");
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_NO_COMMENT;
        }
    }
}
